package com.ouheng.shop.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ouheng.common.annotation.Log;
import com.ouheng.common.config.Constant;
import com.ouheng.common.domain.DictDO;
import com.ouheng.common.service.DictService;
import com.ouheng.common.utils.PageUtils;
import com.ouheng.common.utils.Query;
import com.ouheng.common.utils.R;
import com.ouheng.shop.domain.AdDO;
import com.ouheng.shop.domain.ProductDO;
import com.ouheng.shop.domain.ProductGroupDO;
import com.ouheng.shop.service.AdService;
import com.ouheng.shop.service.ProductGroupService;
import com.ouheng.shop.service.ProductService;

/**
 * 
 * 栏目(广告位)
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2018-06-22 13:54:15
 */
 
@Controller
@RequestMapping("/shop/ad")
public class AdController {
	
	@Autowired
	private ProductService productService;
	
	@Autowired
	private AdService adService;
	
	@Autowired
	private DictService dictService;
	
	@Autowired
	private ProductGroupService productGroupService;
	
	@GetMapping()
	@RequiresPermissions("shop:ad:ad")
	String Ad(){
	    return "shop/ad/ad";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("shop:ad:ad")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<AdDO> adList = adService.list(query);
		int total = adService.count(query);
		PageUtils pageUtils = new PageUtils(adList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("shop:ad:add")
	String add(Model model){
//		List<PreProductDO> listPreProduct = preProductService.list(null);
		List<ProductDO> listPreProduct = productService.list(null);
		List<DictDO> listDict = dictService.listTypeOrderBySort("adposition_type");
		model.addAttribute("listPreProduct", listPreProduct);
		model.addAttribute("listDict", listDict);
	    return "shop/ad/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("shop:ad:edit")
	String edit(@PathVariable("id") Long id,Model model){
		AdDO ad = adService.get(id);
		List<DictDO> listDict = dictService.listTypeOrderBySort("adposition_type");
		if("1".equals(ad.getType())){
			//商品
			List<ProductDO> listPreProduct = productService.list(null);
			model.addAttribute("listPreProduct", listPreProduct);
		}else if("2".equals(ad.getType())){
			//商品分类
			List<ProductGroupDO> listPreProduct = productGroupService.list(null);
			model.addAttribute("listPreProduct", listPreProduct);
		}
		model.addAttribute("listDict", listDict);
		model.addAttribute("ad", ad);
		model.addAttribute("Oss_Url", Constant.Oss_Url);
	    return "shop/ad/edit";
	}
	
	/**
	 * 保存
	 */
	@Log("添加广告位")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("shop:ad:add")
	public R save( AdDO ad){
		if(adService.save(ad)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@Log("编辑广告位")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("shop:ad:edit")
	public R update( AdDO ad){
		adService.update(ad);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@Log("删除广告位")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("shop:ad:remove")
	public R remove( Long id){
		if(adService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@Log("删除广告位")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("shop:ad:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ids){
		adService.batchRemove(ids);
		return R.ok();
	}
	
	
	@GetMapping("/selectUrl")
	@ResponseBody
	R selectUrl(String type,Model model){
		Map<String, Object> map = new HashMap<>();
		if("1".equals(type)){
			List<ProductDO> listPreProduct = productService.list(null);
			map.put("listPreProduct", listPreProduct);
		}else if("2".equals(type)){
			List<ProductGroupDO> listPreProduct = productGroupService.list(null);
			map.put("listPreProduct", listPreProduct);
		}
	    return R.ok(map);
	}
}
